package com.chilli.cosima.action.usermanager;

import java.util.List;
import java.util.Map;

import com.chilli.cosima.action.BaseSupport;
import com.chilli.cosima.data.bean.TPower;
import com.chilli.cosima.data.bean.TPowerRole;
import com.chilli.cosima.data.bean.TRole;
import com.chilli.cosima.service.UserService;

public class RoleQueryAction extends BaseSupport
{
	/**
	 * 注释内容
	 */
	private static final long serialVersionUID = 1937262833782091566L;

	private UserService userService;

	private Long id;

	private TRole role;

	public String detail()
	{
		TRole role = userService.findRoleById(id);

		if (null != role)
		{
			try
			{
				this.role = role;

				// 获得所有权限
				List<TPower> powerlist = userService.findAllPowers();

				// 获得用户对应角色的权限
				List<TPowerRole> optpowerlist = userService
						.findRolePowerByRoleId(this.role.getId());

				// 创建用户权限树
				userService.clearUpdatePowerTreeList();
				userService.updateTree("0", optpowerlist, powerlist);

				// 获得权限树
				List<String> updatePowerTreeList = userService
						.getUpdatePowerTreeList();
				super.request.setAttribute("updatePowerTreeList",
						updatePowerTreeList);
				return DETAIL;
			} catch (Exception e)
			{
				logger.error("error", e);
			}

			return ERROR;
		} else
		{
			logger.error("error");
			return ERROR;
		}

	}

	@SuppressWarnings("unchecked")
	public String execute()
	{

		String page = request.getParameter("page");
		int current = 0;
		if (page != null && !"".equals(page))
		{
			current = Integer.parseInt(page);
		}

		Map map = userService.findRoleByPage(current, pageSize);
		List list = (List) map.get("list");
		int count = (Integer) map.get("count");

		setPager(list, "user/roleQueryAction.action", count);

		return SUCCESS;
	}

	public UserService getUserService()
	{
		return userService;
	}

	public void setUserService(UserService userService)
	{
		this.userService = userService;
	}

	public Long getId()
	{
		return id;
	}

	public void setId(Long id)
	{
		this.id = id;
	}

	public TRole getRole()
	{
		return role;
	}

	public void setRole(TRole role)
	{
		this.role = role;
	}

}
